package com.chenqi.library.recycleview.base

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


/**
 * 作者: CQ
 * 日期: 2021-03-16
 * 说明:recycleView适配器的基础封装，可直接调用
 */
abstract class BaseRecycleViewAdapter<T>(var context: Context, var list: MutableList<T>) : RecyclerView.Adapter<BaseViewHolder>() {

    private var onItemClickListener: OnItemClickListener? = null
    private var onItemLongClickListener: OnItemLongClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val view: View = LayoutInflater.from(context).inflate(getInflate(), null)
        val viewHolder = BaseViewHolder(view)
        view.setOnClickListener {
            if (onItemLongClickListener != null) {
                onItemClickListener!!.onItemClickListener(view, viewHolder.adapterPosition)
            }
        }
        view.setOnLongClickListener {
            if (onItemLongClickListener != null) {
                onItemLongClickListener!!.onItemLongClickListener(view, viewHolder.adapterPosition)
            }
            true
        }
        return viewHolder
    }


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        bindData(holder, list[position], position)
    }

    /**
     * 布局
     */
    abstract fun getInflate(): Int

    /**
     * 赋值
     */
    protected abstract fun bindData(holder: BaseViewHolder, data: T, position: Int)


}